<?php
namespace app\common\library;

use think\Env;

class Integrate
{
    // 表单提交字符集编码
    private $postCharset = "UTF-8";

    private $fileCharset = "UTF-8";
    private $key         = 'rm2RQBdxzW5kRKHLKcHkN6XIlUXA1UBBumxv25IWvES';//秘钥
    private $base_url    = 'https://card.pdsyun.com/';
    
     public function __construct($community_id = null)
    {
        $this->base_url = Env::get('app.pdsurl','https://card.pdsyun.com/');
    }

    //机构账户充值
    public function recharge($data)
    {
        $url       = $this->base_url.'goldapi/accountRecharge';
        $result    = $this->sendHttp($url,$data);
        return $result;
    }

    //发券
    public function create($data)
    {
        $url       = $this->base_url.'goldapi/batch';
        $result    = $this->sendHttp($url,$data);
        return $result;
    }
    //营销积分流水记录
    public function log($data)
    {
        $url       = $this->base_url.'goldapi/codeListlog';
        $result    = $this->sendHttp($url,$data);
        return $result;
    }
    //批次列表
    public function stockList($data)
    {
        $url       = $this->base_url.'goldapi/batchList';
        $result    = $this->sendHttp($url,$data);
        return $result;
    }
    //核销码列表
    public function codeList($data)
    {
        $url       = $this->base_url.'goldapi/codeList';
        $result    = $this->sendHttp($url,$data);
        return $result;
    }

    //机构地区列表
    public function areaList($data=[])
    {
        $url       = $this->base_url.'goldapi/configsList';
        $result    = $this->sendHttp($url,$data);
        return $result;
    }
    //门店查询
    public function storeList($data = [])
    {
        $url       = $this->base_url.'goldapi/storeList';
        $result    = $this->sendHttp($url,$data);
        return $result;
    }

    //门店分类查询
    public function storeCategoryList()
    {
        $url       = $this->base_url.'goldapi/storeCatgories';
        $result    = $this->sendHttp($url,[]);
        return $result;
    }

    //激活积分券
    public function activate($data)
    {
        $url       = $this->base_url.'goldapi/status';
        $result    = $this->sendHttp($url,$data);
        return $result;
    }

    //添加商户
    public function store_add($data)
    {
        $url       = $this->base_url.'goldapi/storeAdd';
        $result    = $this->sendHttp($url,$data);
        return $result;
    }

    //修改券标题图片
    public function edit_batch($data){
        $url       = $this->base_url.'goldapi/editbatch';
        $result    = $this->sendHttp($url,$data);
        return $result;
    }


    public function add_card($data)
    {
        $url       = $this->base_url.'goldapi/addcard';
        $result    = $this->sendHttp($url,$data);
        return $result;
    }



    protected function sendHttp($url,$data)
    {
        $sign_data = $this->get_data($data);
        $response  = $this->curl_get($url,$sign_data);
        $res       = json_decode($response, true);
        return $res;
    }

    protected function curl_get($url, $data)
    {
        $ch = curl_init();
        $header = ['Content-type: application/json'];
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        $output = curl_exec($ch);
        curl_close($ch);

        return $output;
    }

    /**
     * 校验$value是否非空
     *  if not set ,return true;
     *    if is null , return true;
     **/
    protected function checkEmpty($value)
    {
        if (!isset($value))
            return true;
        if ($value === null)
            return true;
        if (trim($value) === "")
            return true;

        return false;
    }

    //提交数据sha1加密
    public function get_data($data)
    {
        $key = $this->key;
        $string = $this->getSignContent($data) . '&key=' . $key;
        $data['sign'] = sha1($string);

        return json_encode($data);
    }

    //校验sha1
    public function check_sha1($data)
    {
        $key = $this->key;
        $sign = $data['sign'];
        $data['sign'] = null;
        $string = $this->getSignContent($data) . '&key=' . $key;

        if ($sign == sha1($string)) {
            return [
                'code' => 1,
                'msg' => '验证通过'
            ];
        } else {
            return [
                'code' => 0,
                'msg' => '验证不通过'
            ];
        }
    }

    /**
     * 转换字符集编码
     * @param $data
     * @param $targetCharset
     * @return string
     */
    public function characet($data, $targetCharset)
    {
        if (!empty($data)) {
            $fileType = $this->fileCharset;
            if (strcasecmp($fileType, $targetCharset) != 0) {
                $data = mb_convert_encoding($data, $targetCharset);
                //$data = iconv($fileType, $targetCharset.'//IGNORE', $data);
            }
        }

        return $data;
    }

    /**
     * 格式化参数格式化成url参数
     */
    protected function getSignContent($str)
    {
        $buff = "";
        foreach ($str as $k => $v) {
            if (is_array($v)) {
                $v = json_encode($v, JSON_UNESCAPED_UNICODE);
            }
            if ($k != "sign" && false === $this->checkEmpty($v)) {
                $buff .= $k . "=" . $v . "&";
            }
        }

        $buff = trim($buff, "&");

        return $buff;
    }

}